package com.gdut.simple.view.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ExpandableListView;

import com.gdut.simple.R;
import com.gdut.simple.contract.FavoritesContract;
import com.gdut.simple.data.entity.Favorites;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.presenter.user.FavoritesPresenter;
import com.gdut.simple.view.activity.WebActivity;
import com.gdut.simple.view.adapter.FavoritesAdapter;
import com.gdut.simple.view.basic.AbsBasicActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;

public class FavoritesActivity extends AbsBasicActivity implements FavoritesContract.View {

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_expand_list_view)
    ExpandableListView mExpandListView;
    @BindView(R.id.id_swipe_refresh_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private List<String> mTypes = new ArrayList<>();
    private List<List<Favorites>> mItemList = new ArrayList<>();
    private FavoritesAdapter mFavoritesAdapter;

    private FavoritesContract.Presenter mPresenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_favorites);
        ButterKnife.bind(this);
        new FavoritesPresenter(this);
        initToolbar();
        initRefreshLayout();
        mFavoritesAdapter = new FavoritesAdapter(this, mTypes, mItemList);
        mExpandListView.setAdapter(mFavoritesAdapter);
        mExpandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                Intent intent = new Intent(FavoritesActivity.this, WebActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("type", mItemList.get(i).get(i1).getType());
                bundle.putString("url", mItemList.get(i).get(i1).getUrl());
                bundle.putString("title", mItemList.get(i).get(i1).getTitle());
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }
        });
    }

    private void initToolbar() {
        mToolbar.setTitle(R.string.user_fav);
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimaryDark),
                ContextCompat.getColor(this, R.color.colorPrimary),
                ContextCompat.getColor(this, R.color.colorPrimaryDark));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.loadData();
            }
        });
    }

    @Override
    protected void loadData() {
        mPresenter.loadData();
    }

    @Override
    public User getUser() {
        if (getIntent().getExtras() != null) {
            User user = getIntent().getExtras().getParcelable("user");
            String objectId = getIntent().getExtras().getString("objectId");
            if (user != null && objectId != null && !objectId.isEmpty()) {
                user.setObjectId(objectId);
                return user;
            }
        }
        return BmobUser.getCurrentUser(User.class);
    }

    @Override
    public List<List<Favorites>> getItemList() {
        return mItemList;
    }

    @Override
    public List<String> getTypes() {
        return mTypes;
    }

    @Override
    public void refreshList() {
        if (mFavoritesAdapter != null) {
            mFavoritesAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showRefreshProgress() {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideRefreshProgress() {
        if (mSwipeRefreshLayout != null && mSwipeRefreshLayout.isRefreshing()) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void setPresenter(@NonNull FavoritesContract.Presenter presenter) {
        this.mPresenter = presenter;
    }
}
